﻿using System;
using System.Collections.Generic;
using System.Text;
using RestSharp;
using System.IO;
using System.Net.Http;

namespace EasyClassUI.WebRequest {
    class ResourceService {

        private string ResourceURL = "https://localhost:5001/resource/";

        /*-----------------------资源下载服务类------------------------*/
        
        //上传文件至群组内
        public string UploadFileToGroup(List<string> filesToUpload, string groupID) {
            var client = new RestClient($"{ResourceURL}upload?isGroup=true&ID={groupID}");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            foreach (string file in filesToUpload) {
                request.AddFile("body", file);
            }
            IRestResponse response = client.Execute(request);
            string status = Convert.ToInt32(response.StatusCode).ToString();
            switch (status) {
                case "200": return "200";
                case "400": return "400";
                default: return "404";
            }
        }

        // 追加文件到帖子上
        public string UploadFileToPost(List<string> filesToUpload, string postID) {
            var client = new RestClient($"{ResourceURL}upload?isGroup=false&ID={postID}");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            foreach (string file in filesToUpload) {
                request.AddFile("body", file);
            }
            IRestResponse response = client.Execute(request);
            string status = Convert.ToInt32(response.StatusCode).ToString();
            switch (status) {
                case "200": return "200";
                case "400": return "400";
                default: return "404";
            }
        }

        // 从群组内下载文件
        public async void DownloadFileFromGroup(string Filename, int groupID, string PathToWrite) {
            try {
                string url = @$"{ResourceURL}download?filename={Filename}&isGroup=true&ID={groupID}";
                HttpClient Client = new HttpClient();
                var response = await Client.GetAsync(url);
                response.EnsureSuccessStatusCode();//确保正常运作
                var stream = await response.Content.ReadAsStreamAsync();
                //var filename = response.Content.Headers.ContentDisposition.FileName;
                using (var filestream = new FileStream(@$"{PathToWrite}", FileMode.Create, FileAccess.Write)) {
                    stream.CopyTo(filestream);
                }
            }
            catch (Exception error) {
                Console.WriteLine(error.Message);
            }
        }

        // 从帖子内下载文件
        public async void DownloadFileFromPost(string Filename, int postID, string PathToWrite) {
            try {
                string url = @$"{ResourceURL}download?filename={Filename}&isGroup=false&ID={postID}";
                HttpClient Client = new HttpClient();
                var response = await Client.GetAsync(url);
                response.EnsureSuccessStatusCode();//确保正常运作
                var stream = await response.Content.ReadAsStreamAsync();
                //var filename = response.Content.Headers.ContentDisposition.FileName;
                using (var filestream = new FileStream(@$"{PathToWrite}", FileMode.Create, FileAccess.Write)) {
                    stream.CopyTo(filestream);
                }
            }
            catch (Exception error) {
                Console.WriteLine(error.Message);
            }
        }

        // 从群组内删除文件
        public string DeleteFileFromGroup(string fileName, string groupID) {
            var client = new RestClient($"{ResourceURL}?fileName={fileName}&isGroup=true&ID={groupID}");
            client.Timeout = -1;
            var request = new RestRequest(Method.DELETE);
            IRestResponse response = client.Execute(request);
            string status = Convert.ToInt32(response.StatusCode).ToString();
            switch (status) {
                case "200": return "200";
                case "400": return "400";
                default: return "404";
            }
        }
    }
}
